package com.yuexun.dhdoctor.module.personalPlan.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yuexun.dhdoctor.R;
import com.yuexun.dhdoctor.base.BaseEntity;
import com.yuexun.dhdoctor.config.AppConfig;
import com.yuexun.dhdoctor.module.personalPlan.bean.ScheduleBean;
import com.yuexun.dhdoctor.module.personalPlan.model.impl.PlanModel;
import com.yuexun.dhdoctor.module.personalPlan.presenter.impl.ScheduleDeletePresenter;
import com.yuexun.dhdoctor.module.personalPlan.ui.Act_Schedule_Add;
import com.yuexun.dhdoctor.module.personalPlan.ui.view.IScheduleDeleteView;
import com.yuexun.dhdoctor.widget.DoubleDialog;

import org.kymjs.kjframe.ui.ViewInject;
import org.kymjs.kjframe.utils.PreferenceHelper;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/4/20 0020.
 */

public class MyScheduleAdapter extends BaseExpandableListAdapter implements IScheduleDeleteView {

    List<ScheduleBean> scheduleBeen;
    Context context;
    ScheduleDeletePresenter presenter;

    DoubleDialog dialog;

    int parentPosition;
    int childPosition;

    public MyScheduleAdapter(Context context, List<ScheduleBean> scheduleBeen) {
        this.context = context;
        this.scheduleBeen = scheduleBeen;
        presenter = new ScheduleDeletePresenter(new PlanModel(), this);
    }

    public void setScheduleBeen(List<ScheduleBean> scheduleBeen) {
        this.scheduleBeen = scheduleBeen;
        notifyDataSetInvalidated();
    }

    @Override
    public int getGroupCount() {
        return null == scheduleBeen ? 0 : scheduleBeen.size();
    }

    @Override
    public int getChildrenCount(int i) {
        return 0 == scheduleBeen.get(i).getDatas().size() ? 1 : scheduleBeen.get(i).getDatas().size();
    }

    @Override
    public Object getGroup(int i) {
        return scheduleBeen.get(i);
    }

    @Override
    public Object getChild(int i, int i1) {
        return scheduleBeen.get(i).getDatas().get(i1);
    }

    @Override
    public long getGroupId(int i) {
        return i;
    }

    @Override
    public long getChildId(int i, int i1) {
        return i1;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public View getGroupView(int i, boolean b, View view, ViewGroup viewGroup) {
        ViewGroupHolder holder;
        if (null == view) {
            view = LayoutInflater.from(context).inflate(R.layout.item_parent, null);
            holder = new ViewGroupHolder();
            holder.groupName = (TextView) view.findViewById(R.id.item_group_name);
            view.setTag(holder);
        } else {
            holder = (ViewGroupHolder) view.getTag();
        }

        holder.groupName.setText(scheduleBeen.get(i).getMonth() + "  " + scheduleBeen.get(i).getWeek());
        return view;
    }

    @Override
    public View getChildView(final int i, final int i1, boolean b, View view, ViewGroup viewGroup) {
        ViewChildHolder holder;
        if (null == view) {
            view = LayoutInflater.from(context).inflate(R.layout.item_schedule, null);
            holder = new ViewChildHolder();
            holder.schedule_time = (TextView) view.findViewById(R.id.schedule_time);
            holder.schedule_title = (TextView) view.findViewById(R.id.schedule_title);
            holder.schedule_delete = (TextView) view.findViewById(R.id.schedule_delete);
            holder.add_schedule = (LinearLayout) view.findViewById(R.id.add_schedule);
            holder.schedule_view = (LinearLayout) view.findViewById(R.id.schedule_view);
            holder.remind_txt = (TextView) view.findViewById(R.id.remind_txt);
            holder.noneTxt = (TextView) view.findViewById(R.id.noneTxt);
            view.setTag(holder);
        } else {
            holder = (ViewChildHolder) view.getTag();
        }

        holder.noneTxt.setVisibility(View.GONE);

        if (0 == scheduleBeen.get(i).getDatas().size()) {
            holder.schedule_view.setVisibility(View.GONE);
            holder.add_schedule.setVisibility(View.VISIBLE);
        } else if (scheduleBeen.get(i).getDatas().size() > 0) {
            holder.schedule_view.setVisibility(View.VISIBLE);
            holder.add_schedule.setVisibility(View.GONE);
            if (i1 == scheduleBeen.get(i).getDatas().size() - 1) {
                holder.add_schedule.setVisibility(View.VISIBLE);
            }

            if (!scheduleBeen.get(i).getDatas().get(i1).getRemindTime().isEmpty()) {
                try {
                    Date time = new SimpleDateFormat("yyyy-MM-dd HH:mm").
                            parse(scheduleBeen.get(i).getDatas().get(i1).getRemindTime());
                    String s = new SimpleDateFormat("MM-dd").format(time) + "\n" + new SimpleDateFormat("HH:mm").format(time);
                    holder.schedule_time.setText(s);
                    holder.remind_txt.setText("提醒时间：");
                } catch (ParseException e) {
                    e.printStackTrace();
                }
            } else {
                holder.remind_txt.setText("不提醒");
                holder.schedule_time.setText("");
            }
            holder.schedule_title.setText(scheduleBeen.get(i).getDatas().get(i1).getContent());
        }

        try {
            Long date = new SimpleDateFormat("yyyy-MM-dd").parse(scheduleBeen.get(i).getDate()).getTime();
            String today = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
            if (date < new SimpleDateFormat("yyyy-MM-dd").parse(today).getTime()) {
                holder.add_schedule.setVisibility(View.GONE);
                if (0 == scheduleBeen.get(i).getDatas().size()) {
                    holder.noneTxt.setVisibility(View.VISIBLE);
                }
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }

        holder.add_schedule.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent_lock = new Intent(context, Act_Schedule_Add.class);
                intent_lock.putExtra("type", "add");
                intent_lock.putExtra("datetime", scheduleBeen.get(i).getDate());
                intent_lock.putExtra("week", scheduleBeen.get(i).getWeek());
                context.startActivity(intent_lock);
            }
        });

        holder.schedule_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                parentPosition = i;
                childPosition = i1;

                dialog = new DoubleDialog(context).builder().setTitle("确定删除日程！");
                dialog.setCallBack(new DoubleDialog.ICallBack() {
                    @Override
                    public void sureCallback() {
                        Map<String, String> params = new HashMap<>();
                        params.put("sessionUuid", PreferenceHelper.readString(context, AppConfig.USER_INFO, AppConfig.SESSION_ID));
                        params.put("scheduleId", scheduleBeen.get(i).getDatas().get(i1).getScheduleId());
                        presenter.deleteSchedule(params);
                    }

                    @Override
                    public void cancelCallback() {
                        dialog.dismiss();
                    }
                });
                dialog.show();
            }
        });

        return view;
    }

    @Override
    public boolean isChildSelectable(int i, int i1) {
        return true;
    }

    @Override
    public void deleteSchedule(BaseEntity baseEntity) {
        if (baseEntity.getStatus().equals("200")) {
            dialog.dismiss();
            scheduleBeen.get(parentPosition).getDatas().remove(childPosition);
            notifyDataSetChanged();
        } else {
            ViewInject.toast(baseEntity.getMessage());
        }
    }

    class ViewGroupHolder {
        TextView groupName;
    }

    class ViewChildHolder {
        TextView schedule_time, schedule_title, schedule_delete, remind_txt, noneTxt;
        LinearLayout schedule_view, add_schedule;
    }
}
